package com.itmk.web.adminInfo.controller;

import com.itmk.utils.Result;
import com.itmk.web.adminInfo.entity.EduInfo;
import com.itmk.web.adminInfo.entity.ForjInfo;
import com.itmk.web.adminInfo.service.AdminService;
import com.itmk.web.ai.entity.Pdfs;
import com.itmk.web.companyInfo.entity.Company;
import com.itmk.web.companyInfo.entity.JobInfo;
import com.itmk.web.user.entity.UserLogin;
import com.itmk.web.userInfo.entity.BasicInfo;
import com.itmk.web.userInfo.entity.JobExperience;
import com.itmk.web.userInfo.entity.ProjectExperience;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //检索用户信息
    @RequestMapping("/getAllUser")
    @ResponseBody
    public Result getAllUser(String signType){
        List<UserLogin> allUser = adminService.getAllUser(signType);
        if(allUser!=null){
            return Result.ok().put("data",allUser);
        }
        return Result.error("查询用户信息失败");
    }
//---------------------------------------------------basicInfo-----------------------------------------------------------------------

    //根据UUID检索用户的基础信息
    @RequestMapping("/getBasicByUUID")
    @ResponseBody
    public Result getBasicByUUID(String uuid){
        BasicInfo basicInfo = adminService.getBasicInfo(uuid);
        if(basicInfo!=null){
            return Result.ok().put("data",basicInfo);
        }
        return Result.error("查询信息失败");
    }

    //根据UUID更新用户的基础信息
    @RequestMapping("/updateBasicByUUID")
    @ResponseBody
    public Result updateBasicByUUID(@RequestBody BasicInfo basicInfo){
        Boolean result = adminService.updateBasicInfo(basicInfo);
        if(result!=null){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    //根据UUID删除用户的基础信息
    @RequestMapping("/deleteBasicByUUID")
    @ResponseBody
    public Result deleteBasicByUUID(String uuid){
        Boolean result = adminService.deleteBasicInfo(uuid);
        if(result!=null){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    //根据UUID增加用户的基础信息
    @RequestMapping("/addBasicByUUID")
    @ResponseBody
    public Result addBasicByUUID(String uuid){
        //检索uuid在表中是否存在
        UserLogin userLogin = adminService.getUser(uuid);
        //检索在basicInfo表中是否存在
        BasicInfo basicInfo = adminService.getBasicInfo(uuid);
        if(userLogin!=null&&basicInfo==null){
            BasicInfo basic = new BasicInfo();
            basic.setPhone(userLogin.getPhone());
            basic.setPassword(userLogin.getPassword());
            basic.setSignType(userLogin.getSignType());
            basic.setUuid(uuid);
            Boolean res = adminService.addBasicInfo(basic);
            if(res){
                return Result.ok("添加成功");
            }
            return Result.error("添加失败");
        }
        return Result.error("账号不存在或者账号基础信息已存在");
    }
//---------------------------------------------------basicInfo-----------------------------------------------------------------------



//---------------------------------------------------educationInfo-----------------------------------------------------------------------

    //根据UUID检索用户的学历信息

    @RequestMapping("/getEduByUUID")
    @ResponseBody
    public Result getEduByUUID(String uuid){
        EduInfo eduInfo = adminService.getEduInfo(uuid);
        if(eduInfo!=null){
            return Result.ok().put("data",eduInfo);
        }
        return Result.error("查询信息失败");
    }

    //根据UUID更新用户的学历信息
    @RequestMapping("/updateEduByUUID")
    @ResponseBody
    public Result updateEduByUUID(@RequestBody EduInfo eduInfo){
        Boolean result = adminService.updateEduInfo(eduInfo);
        if(result!=null){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    //根据UUID删除用户的学历信息
    @RequestMapping("/deleteEduByUUID")
    @ResponseBody
    public Result deleteEduByUUID(String uuid){
        Boolean result = adminService.deleteEduInfo(uuid);
        if(result!=null){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    //根据UUID增加用户的基础信息
    @RequestMapping("/addEduByUUID")
    @ResponseBody
    public Result addEduByUUID(String uuid){
        //检索uuid在表中是否存在
        UserLogin userLogin = adminService.getUser(uuid);
        //检索在basicInfo表中是否存在
        EduInfo eduInfo = adminService.getEduInfo(uuid);
        if(userLogin!=null&&eduInfo==null){
            EduInfo educationInfo = new EduInfo();
            educationInfo.setUuid(uuid);
            Boolean res = adminService.addEduInfo(educationInfo);
            if(res){
                return Result.ok("添加成功");
            }
            return Result.error("添加失败");
        }
        return Result.error("账号不存在或者账号基础信息已存在");
    }

    //---------------------------------------------------educationInfo-----------------------------------------------------------------------


    //---------------------------------------------------forJobInfo-----------------------------------------------------------------------
    //根据UUID检索用户的期望职位信息
    @RequestMapping("/getForJobByUUID")
    @ResponseBody
    public Result getForJobByUUID(String uuid){
        ForjInfo fjInfo = adminService.getForJobInfo(uuid);
        if(fjInfo!=null){
            return Result.ok().put("data",fjInfo);
        }
        return Result.error("查询信息失败");
    }

    //根据UUID更新用户的期望职位信息
    @RequestMapping("/updateForJobUUID")
    @ResponseBody
    public Result updateForJobByUUID(@RequestBody ForjInfo forjInfo){
        Boolean result = adminService.updateForJobInfo(forjInfo);
        if(result!=null){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    //根据UUID删除用户的期望职位信息
    @RequestMapping("/deleteForJobByUUID")
    @ResponseBody
    public Result deleteForJobByUUID(String uuid){
        Boolean result = adminService.deleteForJobInfo(uuid);
        if(result!=null){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    //根据UUID增加用户的基础信息
    @RequestMapping("/addForJobByUUID")
    @ResponseBody
    public Result addForJobByUUID(String uuid){
        //检索uuid在表中是否存在
        UserLogin userLogin = adminService.getUser(uuid);
        //检索在basicInfo表中是否存在
        ForjInfo forjInfo = adminService.getForJobInfo(uuid);
        if(userLogin!=null&&forjInfo==null){
            ForjInfo forjInfo1 = new ForjInfo();
            forjInfo1.setUuid(uuid);
            Boolean res = adminService.addForJobInfo(forjInfo1);
            if(res){
                return Result.ok("添加成功");
            }
            return Result.error("添加失败");
        }
        return Result.error("账号不存在或者账号基础信息已存在");
    }


    //---------------------------------------------------forjobInfo-----------------------------------------------------------------------


    //---------------------------------------------------ResumePDF-----------------------------------------------------------------------
    //根据UUID检索用户的简历PDF版
    @RequestMapping("/getResumePDF")
    @ResponseBody
    public Result getResumePDF(String uuid){
        Pdfs pdfs = adminService.getResumePdf(uuid);
        if(pdfs!=null){
            return Result.ok().put("data",pdfs);
        }
        return Result.error("查询信息失败");
    }

    //根据UUID来跟新用户的简历
    @RequestMapping("/updateResumePDF")
    @ResponseBody
    public Result updateResumePDF(MultipartFile resume,String uuid){
        if(resume!=null) {
            //判断图片是否存在
            String exist = adminService.checkFile(uuid);
            if (exist != null) {
                String pre = "E:\\file\\pdfs\\resume\\";
                File file = new File(pre + exist);
                if (file.delete()) {
                    System.out.println("删除成功");
                } else {
                    System.out.println("删除失败");
                    return Result.error("替换原有图片操作失败");
                }
            }

            //file重命名
            String originalFilename = resume.getOriginalFilename();
            String ext = "." + originalFilename.split("\\.")[1];
            String imageUUID = UUID.randomUUID().toString().replace("-", "");
            String fileName = imageUUID + ext;
            //上传图片
//            ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//            String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+
//                    "\\src\\main\\resources\\static\\images\\companyLogo\\";
//            String property = System.getProperty("user.dir")+"\\itmk-base-web";
//            String pre = property + "\\target\\classes\\static\\pdfs\\";

            String pre = "E:\\file\\pdfs\\resume\\";
            adminService.updateResume(fileName, uuid);
            String path1 = pre + fileName;
            String path = path1.replace("/", "\\");
            try {
                resume.transferTo(new File(path));
                return Result.ok("更新简历成功");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return Result.error("上传文件不存在");
    }
    //根据UUID来删除用户的简历
    @RequestMapping("/deleteResumePDF")
    @ResponseBody
    public Result deleteResumePDF(String uuid){
        Boolean res = adminService.deleteResumePDF(uuid);
        if(res){
            return Result.ok("删除信息成功");
        }
        return Result.error("删除信息失败");
    }

    //根据UUID来添加用户的简历
    @RequestMapping("/addResumePDF")
    @ResponseBody
    public Result addResumePDF(String uuid){
        //检索uuid在表中是否存在
        UserLogin userLogin = adminService.getUser(uuid);
        //检索在basicInfo表中是否存在
        ForjInfo forjInfo = adminService.getForJobInfo(uuid);
        if(userLogin!=null&&forjInfo==null){
            ForjInfo forjInfo1 = new ForjInfo();
            forjInfo1.setUuid(uuid);
            Boolean res = adminService.addForJobInfo(forjInfo1);
            if(res){
                return Result.ok("添加成功");
            }
            return Result.error("添加失败");
        }
        return Result.error("账号不存在或者账号基础信息已存在");
    }

    //---------------------------------------------------ResumePDF-----------------------------------------------------------------------


    //---------------------------------------------------ProjectExperience-----------------------------------------------------------------------
    //根据UUID检索所有用户的项目经验
    @RequestMapping("/getProExperience")
    @ResponseBody
    public Result getProExperience(String uuid){
        List<ProjectExperience> pe = adminService.getProExperience(uuid);
        if(pe!=null){
            return Result.ok().put("data",pe);
        }
        return Result.error("查询信息失败");
    }

    //根据UUID和ID来修改用户的项目经验
    @RequestMapping("/updateProExperience")
    @ResponseBody
    public Result updateProExperience(@RequestBody ProjectExperience pe){
        Boolean res = adminService.updateProExerience(pe);
        if(res){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    //根据UUID和ID来删除用户的项目经验
    @RequestMapping("/deleteProExperience")
    @ResponseBody
    public Result deleteProExperience(@RequestBody ProjectExperience pe){
        Boolean res = adminService.deleteProExerience(pe);
        if(res){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    //根据UUID和ID来增加用户的项目经验
    @RequestMapping("/addProExperience")
    @ResponseBody
    public Result addProExperience(@RequestBody ProjectExperience pe){
        Boolean res = adminService.addProExerience(pe);
        if(res){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    //---------------------------------------------------ProjectExperience-----------------------------------------------------------------------


    //---------------------------------------------------JobExperience-----------------------------------------------------------------------
    //根据UUID检索所有用户的工作经验
    @RequestMapping("/getJobExperience")
    @ResponseBody
    public Result getJobExperience(String uuid){
        List<JobExperience> pe = adminService.getJobExperience(uuid);
        if(pe!=null){
            return Result.ok().put("data",pe);
        }
        return Result.error("查询信息失败");
    }

    //根据UUID和ID来修改用户的项目经验
    @RequestMapping("/updateJobExperience")
    @ResponseBody
    public Result updateJobExperience(@RequestBody JobExperience je){
        Boolean res = adminService.updateJobExerience(je);
        if(res){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    //根据UUID和ID来删除用户的项目经验
    @RequestMapping("/deleteJobExperience")
    @ResponseBody
    public Result deleteJobExperience(@RequestBody JobExperience je){
        Boolean res = adminService.deleteJobExerience(je);
        if(res){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    //根据UUID和ID来增加用户的项目经验
    @RequestMapping("/addJobExperience")
    @ResponseBody
    public Result addJobExperience(@RequestBody JobExperience je){
        Boolean res = adminService.addJobExerience(je);
        if(res){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    //---------------------------------------------------JobExperience-----------------------------------------------------------------------



    //*******************************************************************************************************************************
    //-----------------------------------------------------------BOSS----------------------------------------------------------------
    //*******************************************************************************************************************************



    //---------------------------------------------------Company-----------------------------------------------------------------------
    //获取具体公司信息
    @RequestMapping("/getCompanyInfo")
    @ResponseBody
    public Result getCompanyInfo(String uuid){
        Company company = adminService.getCompanyInfo(uuid);
        if(company!=null){
            return Result.ok().put("data",company);
        }
        return Result.error("获取数据失败");
    }

    //修改公司信息
    @RequestMapping("/updateComapnyInfo")
    @ResponseBody
    public Result updateComapnyInfo(@RequestBody Company company){
        Boolean res = adminService.updateCompanyInfo(company);
        if(res){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    //修改公司LOGO
    @RequestMapping("/updateComapnyLOGO")
    @ResponseBody
    public Result updateComapnyLOGO(String uuid,MultipartFile pic){
        if(pic!=null){
            //判断图片是否存在
            String exist = adminService.checkPic(uuid);
            if (exist!=null) {
                String pre = "E:\\file\\images\\companyLogo\\";

                File file = new File(pre+exist);
                if(file.delete()){
                    System.out.println("删除成功");
                }else{
                    System.out.println("删除失败");
                    return Result.error("替换原有图片操作失败");
                }
            }

            //file重命名
            String originalFilename = pic.getOriginalFilename();
            String ext = "."+originalFilename.split("\\.")[1];
            String imageUUID = UUID.randomUUID().toString().replace("-","");
            String fileName = imageUUID+ext;
            //上传图片
//            ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//            String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+
//                    "\\src\\main\\resources\\static\\images\\companyLogo\\";
//            String property = System.getProperty("user.dir")+"\\itmk-base-web";
//            String pre = property + "\\target\\classes\\static\\images\\companyLogo\\";
            String pre = "E:\\file\\images\\companyLogo\\";

            adminService.updatePic(fileName,uuid);
            String path1 = pre+fileName;
            String path = path1.replace("/","\\");
            try {
                pic.transferTo(new File(path));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return Result.ok("修改成功").put("logo",fileName);
        }
        return Result.error("上传图片失败");
    }
    //---------------------------------------------------Company-----------------------------------------------------------------------


    //---------------------------------------------------JobInfo-----------------------------------------------------------------------
    //获取公司发布岗位信息
    @RequestMapping("/getJobInfo")
    @ResponseBody
    public Result getJobInfo(String uuid){
        List<JobInfo> res = adminService.getJobInfo(uuid);
        if(res!=null){
            return Result.ok().put("data",res);
        }
        return Result.error("获取岗位信息失败");
    }

    @RequestMapping("/getJobOneInfo")
    @ResponseBody
    public Result getJobOneInfo(String uuid,String jobUUID){
        JobInfo res = adminService.getJobOneInfo(uuid,jobUUID);
        if(res!=null){
            return Result.ok().put("data",res);
        }
        return Result.error("获取岗位信息失败");
    }

    //更改公司发布岗位信息
    @RequestMapping("/updateJobInfo")
    @ResponseBody
    public Result updateJobInfo(@RequestBody JobInfo jobInfo ){
        Boolean res = adminService.updateJobInfo(jobInfo);
        if(res){
            return Result.ok("修改信息成功");
        }
        return Result.error("修改信息失败");
    }

    //删除公司发布的岗位信息
    @RequestMapping("/delteJobInfo")
    @ResponseBody
    public Result deleteJobInfo(@RequestBody JobInfo jobInfo){
        Boolean res = adminService.deleteJobInfo(jobInfo);
        if(res){
            return Result.ok("删除信息成功");
        }
        return Result.error("删除信息失败");
    }

    //添加公司发布的岗位信息
    @RequestMapping("/addJobInfo")
    @ResponseBody
    public Result addJobInfo(@RequestBody JobInfo jobInfo){
        Boolean res = adminService.addJobInfo(jobInfo);
        if(res){
            return Result.ok("添加信息成功");
        }
        return Result.error("添加信息失败");
    }

    //---------------------------------------------------JobInfo-----------------------------------------------------------------------


    //---------------------------------------------------ResumeOfJob-----------------------------------------------------------------------

    //获取具体岗位信息投递的所有简历信息
    @RequestMapping("/getResumeOfJob")
    @ResponseBody
    public Result getResumeOfJob(String jobUuid){
        //获取岗位所有投递简历的用户
        List<String> ResumeID = adminService.getResumeOfJob(jobUuid);
        //根据用户来检索pdf版简历
        if(ResumeID!=null){
            List<String> resumes = new ArrayList<>();
            for (String resume : ResumeID) {
                Pdfs resumePDF = adminService.getResumePdf(resume);
                String res = resumePDF.getLinkPdf();
                resumes.add(res);
            }
            return Result.ok().put("data",resumes);
        }
        return Result.error("当前岗位无用户投递");
    }
    //增加具体岗位信息投递的所有简历信息

    @RequestMapping("/addResumeOfJob")
    @ResponseBody
    public Result addResumeOfJob(String companyUUID,String userUUID,String jobUUID){
        boolean res = adminService.addResumeOfJob(companyUUID,userUUID,jobUUID);
        if(res){
            return Result.ok("增加简历成功");
        }
        return Result.error("增加简历失败");
    }
    //---------------------------------------------------ResumeOfJob-----------------------------------------------------------------------

    //---------------------------------------------------轮播图-----------------------------------------------------------------------


    //---------------------------------------------------轮播图-----------------------------------------------------------------------
}
